package org.gruposp2p.dnie.client.json;

import java.util.ArrayList;
import java.util.List;
import org.gruposp2p.dnie.client.dto.DocumentToSignOptionDTO;
import org.gruposp2p.dnie.client.dto.DocumentToSignDTO;
import org.gruposp2p.dnie.client.dto.SignedDocumentDTO;
import org.gruposp2p.dnie.client.dto.StatisticDTO;
import org.gruposp2p.dnie.client.dto.StatisticOptionDTO;
import org.gruposp2p.dnie.client.util.DateUtils;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import org.gruposp2p.dnie.client.dto.SignedDocumentCommentDTO;
import static org.gruposp2p.dnie.client.json.JSONConstants.*;

public class JSONToDTO {
	
    public JSONToDTO () {}

    public static DocumentToSignDTO getDocumentToSignDTO(JSONValue jsonValue) {
        DocumentToSignDTO documentToSign = new DocumentToSignDTO();
        JSONObject jsonObject;
        if ((jsonObject = jsonValue.isObject() ) != null) {
            JSONValue value;
            JSONString stringValue;
            String date;
            if ((value = jsonObject.get(DOCUMENT_TO_SIGN_SIGNATURE_DATA_TYPE)) != null)
                documentToSign.type = value.isString().stringValue();
            if ((value = jsonObject.get(DOCUMENT_TO_SIGN_URL)) != null)
                documentToSign.url = value.isString().stringValue();
            if ((value = jsonObject.get(DOCUMENT_TO_SIGN_CONTENT)) != null)
                documentToSign.content = value.isString().stringValue();
            if ((value = jsonObject.get(DOCUMENT_TO_SIGN_TITLE)) != null)
                documentToSign.title = value.isString().stringValue();
            if ((value = jsonObject.get(DOCUMENT_TO_SIGN_NUMBER_SIGNATURES)) != null)
                documentToSign.numberSignatures = value.isString().stringValue();
            if ((value = jsonObject.get(DOCUMENT_TO_SIGN_ACTIVE)) != null)
                documentToSign.active = Boolean.valueOf(value.isString().stringValue());
            if ((stringValue = jsonObject.get(
                    DOCUMENT_TO_SIGN_DATE_BEGIN).isString()) != null) {
                date = stringValue.isString().stringValue();
                documentToSign.dateBegin = DateUtils.getDateFromString(date);
            }
            if ((stringValue = jsonObject.get(
                    DOCUMENT_TO_SIGN_DATE_CREATED).isString()) != null) {
                date = stringValue.isString().stringValue();
                documentToSign.dateCreated = DateUtils.getDateFromString(date);
            }            
            if ((stringValue = jsonObject.get(
                    DOCUMENT_TO_SIGN_DATE_END).isString()) != null) {
                date = stringValue.isString().stringValue();
                documentToSign.dateEnd = DateUtils.getDateFromString(date);
            }
            if ((value = jsonObject.get(DOCUMENT_TO_SIGN_OPTIONS)) != null)
                documentToSign.options = getOptionsDTO(value);
            if ((value = jsonObject.get(DOCUMENT_TO_SIGN_ID)) != null)
                documentToSign.id = value.isString().stringValue();
        }       
        return documentToSign;
    }

    public static List<DocumentToSignDTO> getDocumentsToSignDTO (JSONValue jsonValue) {
        List<DocumentToSignDTO> documents = null;
        JSONObject jsonObject;
        JSONValue jsonDocumentToSignValue;
        JSONValue objectReceived;
        JSONArray jsonArray;
        if ((jsonObject = jsonValue.isObject()) != null) {
        	objectReceived = jsonObject.get(DOCUMENT_TO_SIGN);
            documents = new ArrayList<DocumentToSignDTO>();
            if ((jsonArray = objectReceived.isArray()) != null) {
                for (int i = 0; i < jsonArray.size(); ++i) {
                    jsonDocumentToSignValue = jsonArray.get(i);
                    DocumentToSignDTO document =
                        getDocumentToSignDTO(jsonDocumentToSignValue);
                    documents.add(document);
                }
            } else if ((objectReceived.isObject()) != null) {
            	DocumentToSignDTO document =
                    getDocumentToSignDTO(objectReceived);
            	documents.add(document);
            }
        }
        return documents;
    }
	
    public static List<DocumentToSignOptionDTO> getOptionsDTO (JSONValue jsonValue) {
        List<DocumentToSignOptionDTO> options = null;
        JSONObject jsonObject;
        JSONArray jsonArray;
        JSONValue jsonOptionValue;
        if ((jsonObject = jsonValue.isObject()) != null) {
        	jsonOptionValue = jsonObject.get(DOCUMENT_TO_SIGN_OPTION);
        	options = new ArrayList<DocumentToSignOptionDTO>();
        	if ((jsonArray = jsonOptionValue.isArray()) != null) {
                for (int i = 0; i < jsonArray.size(); ++i) {
                    jsonOptionValue = jsonArray.get(i);
                    DocumentToSignOptionDTO optionDTO = getOptionDTO(jsonOptionValue);
                    options.add(optionDTO);
                }
        	}
        }
        return options;
    }
	
    public static List<SignedDocumentCommentDTO> getCommentsDTO (JSONValue jsonValue) {
        List<SignedDocumentCommentDTO> comments = null;
        JSONArray jsonArray;
        JSONValue jsonCommentValue;
        if ((jsonArray = jsonValue.isArray()) != null) {
            comments = new ArrayList<SignedDocumentCommentDTO>();
            for (int i = 0; i < jsonArray.size(); ++i) {
                jsonCommentValue = jsonArray.get(i);
                SignedDocumentCommentDTO commentDTO = getCommentDTO(jsonCommentValue);
                comments.add(commentDTO);
            }
        }
        return comments;
    }
	
    public static DocumentToSignOptionDTO getOptionDTO (JSONValue jsonValue) {
        DocumentToSignOptionDTO optionDTO = null;
        JSONObject jsonObject;
        if ((jsonObject = jsonValue.isObject() ) != null) {
            optionDTO = new DocumentToSignOptionDTO();
            optionDTO.content =
                    jsonObject.get(DOCUMENT_TO_SIGN_OPTION_CONTENT).isString().stringValue();
        }
        return optionDTO;
    }
    
    public static List<SignedDocumentDTO> getSignedDocumentsDTO (JSONValue jsonValue) {
        List<SignedDocumentDTO> documents = null;
        JSONObject jsonObject;
        JSONValue jsonSignedDocumentValue;
        JSONValue objectReceived;
        JSONArray jsonArray;
        if ((jsonObject = jsonValue.isObject()) != null) {
        	objectReceived = jsonObject.get(SIGNED_DOCUMENT);
            documents = new ArrayList<SignedDocumentDTO>();
            if ((jsonArray = objectReceived.isArray()) != null) {
                for (int i = 0; i < jsonArray.size(); ++i) {
                    jsonSignedDocumentValue = jsonArray.get(i);
                    SignedDocumentDTO document =
                    	getSignedDocumentDTO(jsonSignedDocumentValue);
                    documents.add(document);
                }
            } else if ((objectReceived.isObject()) != null) {
            	SignedDocumentDTO document =
            		getSignedDocumentDTO(objectReceived);
            	documents.add(document);
            }
        }
        return documents;
    }


    public static SignedDocumentCommentDTO getCommentDTO (JSONValue jsonValue) {
        SignedDocumentCommentDTO commentDTO = null;
        JSONObject jsonObject;
        JSONString stringValue;
        JSONValue value;
        if ((jsonObject = jsonValue.isObject() ) != null) {
            commentDTO = new SignedDocumentCommentDTO();
            if ((value = jsonObject.get(COMMENT_ID)) != null)
                commentDTO.id = value.isString().stringValue();
            if ((value = jsonObject.get(COMMENT_CONTENT)) != null)
                commentDTO.content = value.isString().stringValue();
            if ((stringValue = jsonObject.get(
                    COMMENT_DATE).isString()) != null) {
                String date = stringValue.isString().stringValue();
                commentDTO.date = DateUtils.getDateFromString(date);
            }
        }
        return commentDTO;
    }

    public static SignedDocumentDTO getSignedDocumentDTO (JSONValue jsonValue) {
    	SignedDocumentDTO signedDocument = null;
        JSONObject jsonObject;
        if ((jsonObject = jsonValue.isObject() ) != null) {
            JSONValue value;
            JSONString stringValue;
            String date;
            if ((value = jsonObject.get(SIGNED_DOCUMENT_ID)) != null)
                signedDocument.id = value.isString().stringValue();
            if ((stringValue = jsonObject.get(
                    SIGNED_DOCUMENT_DATE).isString()) != null) {
                date = stringValue.isString().stringValue();
                signedDocument.date = DateUtils.getDateFromString(date);
            }
            if ((value = jsonObject.get(SIGNED_DOCUMENT_USER_ID)) != null)
                signedDocument.user.id = value.isString().stringValue();
            if ((value = jsonObject.get(SIGNED_DOCUMENT_DOCUMENT_TO_SIGN)) != null)
                signedDocument.documentToSignDTO = getDocumentToSignDTO(value);
            if ((value = jsonObject.get(SIGNED_DOCUMENT_XML_DSIG_DATA)) != null)
                signedDocument.xmlDsigData = value.isString().stringValue();
            if ((value = jsonObject.get(SIGNED_DOCUMENT_COMMENTS)) != null) {
                signedDocument.comments = getCommentsDTO(value);
            }
            if ((value = jsonObject.get(SIGNED_DOCUMENT_SIGNED_OPTIONS)) != null) {
                signedDocument.comments = getCommentsDTO(value);
            }
        }
        return signedDocument;
    }

    public static StatisticDTO getStatisticDTO (JSONValue jsonValue) {
    	StatisticDTO statisticDTO = new StatisticDTO();
        JSONObject jsonObject;
        JSONValue value;
        JSONObject jsonArrayObject;
        JSONArray jsonArray;
        JSONValue optionValue;
        List<StatisticOptionDTO> options = null;
        if ((jsonObject = jsonValue.isObject() ) != null) {
        	if ((value = jsonObject.get(STATISTIC_TOTAL)) != null)
        		statisticDTO.total = value.isString().stringValue();
            if ((jsonArrayObject = jsonObject.get(STATISTIC_OPTIONS).isObject()) != null) {
            	if ((jsonArray = jsonArrayObject.get(STATISTIC_OPTION).isArray()) != null) {
                	options = new ArrayList<StatisticOptionDTO>();
                    for (int i = 0; i < jsonArray.size(); ++i) {
                    	optionValue = jsonArray.get(i);
                    	StatisticOptionDTO optionDTO = getStatisticOptionDTO(optionValue);
                    	options.add(optionDTO);
                    }
                    statisticDTO.options = options;
            	}
            }
        }
    	return statisticDTO;
    }
    
    public static StatisticOptionDTO getStatisticOptionDTO (JSONValue jsonValue) {
    	StatisticOptionDTO statisticOptionDTO = new StatisticOptionDTO();
        JSONObject jsonObject;
        JSONValue value;
        if ((jsonObject = jsonValue.isObject() ) != null) {
        	if ((value = jsonObject.get(STATISTIC_OPTION_NAME)) != null)
        		statisticOptionDTO.name = value.isString().stringValue();
        	if ((value = jsonObject.get(STATISTIC_OPTION_TOTAL)) != null)
        		statisticOptionDTO.total = value.isString().stringValue();        	
        }
    	return statisticOptionDTO;
    }
    

}